package sakila.dal.film.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import sakila.commonprocess.ProcessResult;
import sakila.dal.IDataAcess;
import sakila.dal.commondao.DataAccessHelper;
import sakila.dal.film.table.film;
import sakila.dto.film.BaseDTO;
import sakila.dto.film.FilmDTO;
import sakila.sakilaexcetion.SakilaException;

public class FilmDAO implements IDataAcess
{
	private LanguageDAO languageDao = new LanguageDAO();

	/**
	 * Instantiates a new FilmDAO.
	 */
	public FilmDAO()
	{
	}

	public ProcessResult delete() throws SakilaException
	{
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<FilmDTO> getAll() throws SakilaException
	{
		List<FilmDTO> lstFilms = new ArrayList<FilmDTO>();

		final String queryStr = "SELECT * FROM " + film.TABLE_NAME;
		ResultSet rs = DataAccessHelper.executeQueryNonParameters(queryStr);

		try
		{
			FilmDTO objfilm;

			while (rs.next())
			{
				objfilm = new FilmDTO();
				objfilm.setId(rs.getInt(film.film_id));
				objfilm.setTitle(rs.getString(film.title));
				objfilm.setDescription(rs.getString(film.description));
				objfilm.setReleaseYear(rs.getShort(film.release_year));
				objfilm.setLastUpdate(rs.getDate(film.last_update));
				objfilm.setLength(rs.getShort(film.length));
				objfilm.setLanguage(this.languageDao.getById(rs.getInt(film.language_id)));
				// objfilm.setLanguages(this.languageDao.getById(rs.getInt(film.original_language_id)));
				// objfilm.setRating(rs.getByte(film.rating));
				objfilm.setRentalDuration(rs.getShort(film.rental_duration));
				objfilm.setRentalRate(rs.getFloat(film.rental_rate));
				objfilm.setReplacementCost(rs.getFloat(film.replacement_cost));
				// FIXME set special features

				lstFilms.add(objfilm);
			}
			rs.close();
		}
		catch (Exception e)
		{
			SakilaException.handleException("Fails to get all films", e);
		}
		finally
		{
			DataAccessHelper.closeConnection();
		}
		return lstFilms;
	}

	@SuppressWarnings("unchecked")
	public FilmDTO getById(int id) throws SakilaException
	{
		// TODO Auto-generated method stub
		return null;
	}

	public ProcessResult insert(BaseDTO newRecord) throws SakilaException
	{
		// TODO Auto-generated method stub
		return null;
	}

	public ProcessResult update(BaseDTO record) throws SakilaException
	{
		// TODO Auto-generated method stub
		return null;
	}
}
